package com.ch.engine.design.chain;

/**
 * 验证API密钥是否有效的处理程序
 *
 * @author wangchunhui
 * @date 2023/7/20 10:04
 */
public class ApiKeyValidationHandler implements Handler {
    private Handler nextHandler;

    @Override
    public void setNext(Handler handler) {
        this.nextHandler = handler;
    }

    @Override
    public void handleRequest(Request request) {
        if (isValidApiKey(request.getApiKey())) {
            if (nextHandler != null) {
                nextHandler.handleRequest(request);
            }
        } else {
            System.out.println("Invalid API key!");
        }
    }

    @Override
    public void handleResponse(Response response) {

    }

    private boolean isValidApiKey(String apiKey) {
        // 在此处进行API密钥验证逻辑
        return apiKey.equals("valid_api_key");
    }
}